package com.hummingbird.portal.controller;


import com.hummingbird.portal.pojo.User;
import com.hummingbird.portal.r.R;
import com.hummingbird.portal.service.IUserService;
import com.hummingbird.portal.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author -1E18
 * @since 2024-09-22
 */
@RestController
@RequestMapping("/portal/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/get")
    @PreAuthorize("hasAuthority('/user/get')")
    public User get(Integer id) {
        return userService.getById(id);
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/user/list')")
    public List<User> list() {
        return userService.list();
    }

    @GetMapping("/master")
    public R master() {
        List<User> masters = userService.getMasters();
        return R.ok(masters);
    }

    @GetMapping("/me")
    public R me(@AuthenticationPrincipal UserDetails userDetails) {
        UserVO userVO = userService.getCurrentUserVO(userDetails.getUsername());
        return R.ok(userVO);
    }
}
